package kr.saeildamil.androidstudy_second.multimedia;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.MediaColumns;
import android.provider.MediaStore.Video;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MediaDump extends Activity {
	ContentResolver mCr;
	TextView mResult;
	RadioGroup	mMedia;
	ToggleButton mStorage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_dumpmedia);
		
		mCr = getContentResolver();
		mResult = (TextView)findViewById(R.id.result);
		mStorage = (ToggleButton)findViewById(R.id.storage);
		mMedia = (RadioGroup)findViewById(R.id.media);
		mMedia.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				dumpQuery();
				
			}
		});
	/*	mStorage.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				
			}
		});*/
		dumpQuery();
		
			
	}
	
	BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			dumpQuery();
			
		}
	};

	@Override
	protected void onPause() {
		super.onPause();
		
	}
	@Override
	protected void onResume() {
		super.onResume();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
		filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
		filter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
		filter.addDataScheme("file");
		registerReceiver(mScanReceiver, filter);	
		
	}
	public void dumpQuery(){
		StringBuilder sb = new StringBuilder();
		Uri uri;
		Boolean inStorage = mStorage.isChecked();
		switch(mMedia.getCheckedRadioButtonId()){
		case R.id.image:
		default:
			uri=inStorage?Images.Media.INTERNAL_CONTENT_URI:
				Images.Media.EXTERNAL_CONTENT_URI;
			break;
		case R.id.audio:
			uri=inStorage?Audio.Media.INTERNAL_CONTENT_URI:
				Audio.Media.EXTERNAL_CONTENT_URI;
			break;
		case R.id.video:
			uri=inStorage?Video.Media.INTERNAL_CONTENT_URI:
				Video.Media.EXTERNAL_CONTENT_URI;
			break;
		}
		Cursor cursor = mCr.query(uri, null, null, null, null);
		//select *from uri
		int recordCount = cursor.getColumnCount();//필드의 개수
		sb.append("필드 목록 수=" + recordCount + "\n\n");
		
		for(int i=0; i<recordCount; i++){
			sb.append(i + ":" + cursor.getColumnName(i) + "\n");
		}
		sb.append("\n=========================================\n\n");
		//실제 레코드 값 출력
		sb.append("레코드의 개수는 =" + cursor.getCount() + "\n\n");
		while(cursor.moveToNext()){
			sb.append(getColumeValue(cursor, MediaColumns._ID));
			sb.append(getColumeValue(cursor, MediaColumns.DISPLAY_NAME));
			sb.append(getColumeValue(cursor, MediaColumns.TITLE));
			sb.append(getColumeValue(cursor, MediaColumns.SIZE));
			sb.append(getColumeValue(cursor, MediaColumns.DATE_ADDED));
			sb.append(getColumeValue(cursor, MediaColumns.MIME_TYPE));
			
			switch(mMedia.getCheckedRadioButtonId()){
			case R.id.image:
				sb.append(getColumeValue(cursor, Images.ImageColumns.BUCKET_DISPLAY_NAME));
				sb.append(getColumeValue(cursor, Images.ImageColumns.BUCKET_ID));
				sb.append(getColumeValue(cursor, Images.ImageColumns.DATE_TAKEN));
				sb.append(getColumeValue(cursor, Images.ImageColumns.DESCRIPTION));
				sb.append(getColumeValue(cursor, Images.ImageColumns.IS_PRIVATE));
				sb.append(getColumeValue(cursor, Images.ImageColumns.LATITUDE));
				sb.append(getColumeValue(cursor, Images.ImageColumns.LONGITUDE));
				sb.append(getColumeValue(cursor, Images.ImageColumns.MINI_THUMB_MAGIC));
				sb.append(getColumeValue(cursor, Images.ImageColumns.ORIENTATION));
				break;
			case R.id.audio:
				
				break;
			case R.id.video:
				break;
			}
			
		}
		sb.append("\n");
		mResult.setText(sb.toString());
	}
	private Object getColumeValue(Cursor cursor, String cName) {
		
		return cName + " : " + cursor.getString(cursor.getColumnIndex(cName));
	}
	
}
